package com.dongan.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongan.pojo.Permission;
import com.dongan.pojo.Roles;
import com.dongan.service.PermissionService;
import com.dongan.service.RolesService;
import com.dongan.service.UsersService;
import com.dongan.utils.ResultResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author longTao
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/permission")
@CrossOrigin
public class PermissionController {


    @Autowired
    private RolesService rolesService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UsersService usersService;
    /**
     * 显示角色列表
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PreAuthorize("hasAuthority('/roleQuery')")
    @GetMapping(value = "/queryPermission",produces = {"application/json;charset=utf-8"})
    public ResultResponseUtil queryPermission(String name,String pageNum,String pageSize){
        QueryWrapper<Roles> rolesQueryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            rolesQueryWrapper.like("NAME",name);
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum="1";
        }
        if (StringUtils.isEmpty(pageSize)){
            pageSize="2";
        }
        Page<Roles> page=new Page<>();
        page.setCurrent(Long.valueOf(pageNum));
        page.setSize(Long.valueOf(pageSize));
        Page<Roles> rolesPage = rolesService.page(page, rolesQueryWrapper);
        return new ResultResponseUtil(true,rolesPage);
    }

    /**
     *
     * @param id 角色ID
     * @return
     */
    @CrossOrigin
    @PreAuthorize("hasAuthority('/roleQuery')")
    @GetMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultResponseUtil showPermissionList(@PathVariable Long id){
        List<Permission> list = permissionService.getList();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ID",id);
        List<Roles> roles= rolesService.list(queryWrapper);
        Map<String,Object> map=new HashMap<>();
        if (roles!=null&&!roles.isEmpty()){
            List<Long> roleId = permissionService.getListByRoleId(roles);
            map.put("permissionList",roleId);
        }
        map.put("list",list);
        return new ResultResponseUtil(true,map);
    }

    /**
     * 权限分配
     * @param map
     * @return
     */
    @PreAuthorize("hasAuthority('/permissionAssignment')")
    @PostMapping(value = "/permissionAssignment",produces = {"application/json;charset=utf-8"})
    public ResultResponseUtil permissionAssignment(@RequestBody Map<String,Object> map){
        ObjectMapper mapper=new ObjectMapper();
        try {
            System.out.println("map.get(\"id\").toString() = " + map.get("id").toString());
            System.out.println("map.get(\"arr\").toString() = " + map.get("arr").toString());
            Long id = mapper.readValue(map.get("id").toString(), Long.class);
            Long[] arr=mapper.readValue(map.get("arr").toString(), new TypeReference<Long[]>() {
            });
            System.out.println("id = " + id);
            System.out.println("arr = " + arr);
            permissionService.rightsProfile(id,arr);
            return new ResultResponseUtil(true,"权限分配成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultResponseUtil(false,"权限分配失败!");
        }
    }
}

